package com.cqjtu.email;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/26 0026.
 */
public class ReceiveActivity extends Activity {

    private TextView test;
    private ListView listView;
    private Button reflesh;
    private TextView tex;
    private AlertDialog mDialog;
    private MailAdapter adapter;
    private POP3Main pop3Main ;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.receive_fragment);
        pop3Main = new POP3Main();
        listView = (ListView) findViewById(R.id.id_listView);
        test = (TextView) findViewById(R.id.test);
        reflesh = (Button) findViewById(R.id.btn_reflesh);
        pop3Main = new POP3Main();
        reflesh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (POP3Main.over) {
                    adapter = new MailAdapter(MyApplication.getContext(),R.layout.mail_item,POP3Main.mailList);
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    test.setText("共有"+POP3Main.count+"件邮件！刷新查看！");
                    Log.i("MAIL_SIZE_TWO", "look: "+POP3Main.mailList.size());
                }

            }
        });
        new Thread(network).start();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MailContent mailContent = POP3Main.mailList.get(i);
                LayoutInflater mInflater = LayoutInflater.from(ReceiveActivity.this);
                View dia_view = mInflater.inflate(R.layout.dialog_layout,null);
                AlertDialog.Builder mBuilder = new AlertDialog.Builder(ReceiveActivity.this);
                tex = (TextView) dia_view.findViewById(R.id.id_dialog_tx);
                tex.setText(mailContent.getDetail());
                mBuilder.setTitle("详细信息");
                mBuilder.setView(dia_view);
                mDialog = mBuilder.create();
                mDialog.show();
            }
        });

        Log.i("MAIL_SIZE_TWO", "look: "+POP3Main.mailList.size());

    }



    Runnable network = new Runnable() {
        @Override
        public void run() {


                pop3Main.look();

        }
    };
}
